//index.js
//获取应用实例
const app = getApp()
const host = require('../../config').host

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    lists:"display",
    session:false,
    currentReviewId: 3,
    currentTabId: 0,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  schedules: function (e){
    wx.navigateTo({
      url: `./schedule?id=${e.currentTarget.dataset.id}`
    })
  },
  score: function (e){
    wx.navigateTo({
      url: `./score?id=${e.currentTarget.dataset.id}&article_type=${e.currentTarget.dataset.article_type}`
    })
  },
  toScore: function(e){
    wx.navigateTo({
      url: `../user/adminNewExpert?id=${e.currentTarget.dataset.id}`
    })
  },
  toSearch: function() {
    wx.navigateTo({
      url: './search'
    })
  },
  toadminSearch: function() {
    wx.navigateTo({
      url: './adminSearch'
    })
  },
  readfile: function (e){
    wx.navigateTo({
      url: `./readFile?id=${e.currentTarget.dataset.id}`
    })
  },
  // 评审类型选项卡
  toTables: function (event) {
    console.log(event);
    console.log(event.currentTarget.dataset.typeid);
    // var typeTid = event.currentTarget.dataset.typetid;
    var typeId = event.currentTarget.dataset.typeid;
    // app.globalData.typeId = event.currentTarget.dataset.typeid;
    // var review = event.currentTarget.dataset.review;
    this.setData({
      currentReviewId:event.currentTarget.dataset.typeid,
    });
    this.setData({
      currentTabId:0,
      lists:"display",
      schedule:"",
      listsDetail:"",
      admin_experts:"",
    });
    this.article();
  },
  // 左侧列表选项卡
  toBankuai: function (event) {
    console.log(event);
    console.log(event.currentTarget.dataset.typeid);
    // var typeTid = event.currentTarget.dataset.typetid;
    var typeId = event.currentTarget.dataset.typeid;
    // app.globalData.typeId = event.currentTarget.dataset.typeid;
    // var functions = event.currentTarget.dataset.function;
    this.setData({
      currentTabId:event.currentTarget.dataset.typeid,
      lists:"",
      listsDetail:"",
      schedule:"",
      admin_experts:"",
    });
    if(typeId == "0"){
      this.setData({
        lists:"display",
      });
    }
    if(typeId == "1"){
      this.schedule();
      this.setData({
        schedule:"display",
      });
    }
    if(typeId == "2"){
      this.admin_experts();
      this.setData({
        admin_experts:"display",
      });
    }
  },
  onTabItemTap(item) {
    this.onLoad()
  },
  onTabItemTap(item) {
    this.onLoad()
  },
  onShow: function(){
    // console.log(app.globalData.session)
    var session = app.globalData.session;
    if(!session && typeof(app.globalData.session) != "undefined"){
      wx.switchTab({
        url:"../user/index"
      })
    }
  },
  onLoad: function () {
    var session = app.globalData.session;
    this.setData({
      session:app.globalData.session
    })

    if(session == 2){
      this.expert_pedding();
      this.experts();
    }else if(session == 1){
      this.article();
    }
  },
  listsDetail:function(e){
    this.setData({
      listsDetail:"display",
      lists:"",
      schedule:"",
      admin_experts:"",
    });
    var that = this
    var review_type = this.data.currentReviewId;
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: host+'api/adminreview/listsDetail',
      data:{id:id,review_type:review_type},
      success: function(res){
        that.setData({
          book_content: res.data.book_content
        })
        that.setData({
          expert_c: res.data.expert_c
        })
      },
      fail: function(data){
        // console.log(data)
      }
    })
  },
  schedule:function(){
    var that = this
    var review_type = this.data.currentReviewId;
    wx.request({
      url: host+'api/adminreview/schedule',
      data:{review_type:review_type},
      success: function(res){
        that.setData({
          schedules: res.data
        })
      },
      fail: function(data){
        // console.log(data)
      }
    })
  },
  admin_experts:function(){
    var that = this
    var review_type = this.data.currentReviewId;
    wx.request({
      url: host+'api/adminreview/admin_experts',
      data:{review_type:review_type},
      success: function(res){
        console.log(res.data)
        that.setData({
          admin_expertes: res.data
        })
      },
      fail: function(data){
        // console.log(data)
      }
    })
  },
  article:function(){
    var that = this
    var review_type = this.data.currentReviewId;
    wx.request({
      url: host+'api/adminreview/index',
      data:{review_type:review_type},
      success: function(res){
        that.setData({
          pendingTabs: res.data.works
        })
        that.setData({
          stop_time: res.data.stop_time
        })
      },
      fail: function(data){
        // console.log(data)
      }
    })
  },
  experts: function(){
    var that = this
    wx.request({
      url: host+'api/expertreview/experts',
      data:{expert_id:app.globalData.id},
      success: function(res){
        that.setData({
          experts: res.data
        })
      },
      fail: function(data){
        // console.log(data)
      }
    })
  },
  expert_pedding: function(){
    var that = this
    wx.request({
      url: host+'api/expertreview/index',
      data:{expert_id:app.globalData.id},
      success: function(res){
        console.log(res.data.works)
        that.setData({
          pendingTabs: res.data.works
        })
        that.setData({
          stop_time: res.data.stop_time
        })
      },
      fail: function(data){
        // console.log(data)
      }
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
